﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using System.Net.Http;
using Enough.Http.Cache;
using System.Threading.Tasks;
using Windows.Storage;

namespace Enough.Http.Test
{
    [TestClass]
    public class HttpClientExtensionsTest
    {
        [TestMethod]
        public async Task GetResponseMessage()
        {
            HttpClient client = new HttpClient();
            Uri url = new Uri("http://enough.de");

            await client.ConfigureCacheAsync(cacheSizeInKilobytes: 5 * 1024);

            HttpResponseMessage messageOriginal = await client.GetAsync(url);
            HttpResponseMessage message = await client.GetCachedAsync(url);

            Assert.IsNotNull(message.Content);
            Assert.IsNotNull(messageOriginal.Content.Headers);
            Assert.AreEqual(messageOriginal.Content.Headers.Allow.Count, message.Content.Headers.Allow.Count);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentDisposition, message.Content.Headers.ContentDisposition);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentEncoding.Count, message.Content.Headers.ContentEncoding.Count);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentLanguage.Count, message.Content.Headers.ContentLanguage.Count);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentLength, message.Content.Headers.ContentLength);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentLocation, message.Content.Headers.ContentLocation);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentMD5, message.Content.Headers.ContentMD5);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentRange, message.Content.Headers.ContentRange);
            Assert.AreEqual(messageOriginal.Content.Headers.ContentType, message.Content.Headers.ContentType);
            Assert.AreEqual(messageOriginal.Content.Headers.Expires, message.Content.Headers.Expires);
            Assert.AreEqual(messageOriginal.Content.Headers.LastModified, message.Content.Headers.LastModified);
            Assert.IsNotNull(message.Content.Headers);
            Assert.IsNotNull(message.StatusCode);
            Assert.IsNotNull(message.Headers);
            Assert.IsTrue(message.IsSuccessStatusCode);
        }

        [TestMethod]
        public async Task LoadCachedResponseMessage()
        {
            HttpClient client = new HttpClient();
            Uri url = new Uri("http://enough.de");

            await client.ConfigureCacheAsync(ApplicationData.Current.LocalFolder, cacheSizeInKilobytes: 250);

            HttpResponseMessage messageOriginal = await client.GetCachedAsync(url.ToString());
            HttpResponseMessage message = await client.GetCachedAsync(url.ToString());
        }

        [TestMethod]
        public async Task CheckCacheSize()
        {
            HttpClient client = new HttpClient();
            Uri url = new Uri("http://enough.de");
            Uri url2 = new Uri("http://heise.de");
            Uri url3 = new Uri("http://golem.de");
            Uri url4 = new Uri("http://spiegel.de");

            await client.ConfigureCacheAsync(ApplicationData.Current.LocalFolder, 210);

            HttpResponseMessage message = await client.GetCachedAsync(url);
            await client.GetCachedAsync(url);
            HttpResponseMessage message2 = await client.GetCachedAsync(url2);
            HttpResponseMessage message3 = await client.GetCachedAsync(url3);
            HttpResponseMessage message4 = await client.GetCachedAsync(url4);

            await client.GetCachedAsync(url);
            await client.GetCachedAsync(url2);
            await client.GetCachedAsync(url3);
            await client.GetCachedAsync(url4);
        }
    }
}